﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Runtime.Serialization;
using Framework.Common;
using ProtoBuf;
using Framework.Serializer;
namespace Framework.UnitTest
{
    [TestClass]
    public class ProtoBufSerializerTest
    {
        public TestContext TestContext { get; set; }
        [TestMethod]
        public void Serialize()
        {
            PerformanceHelper.OutputAction = (obj) => TestContext.WriteLine(obj.ToString());
            Person p = new Person
            {
                A1 = "1233556778890",
                A2 = "1233556778890",
                A3 = "1233556778890",
                A4 = "1233556778890",
                A5 = "1233556778890",
                A6 = "1233556778890",
                A7 = "1233556778890",
                A8 = "1233556778890",
                B1 = 1233556778890,
                B2 = 1233556778890,
                B3 = 1233556778890,
                B4 = 1233556778890,
                B5 = 1233556778890,
                B6 = 1233556778890,
                B7 = 1233556778890,
                B8 = 1233556778890
            };
            int times = 10000;

            PerformanceHelper.Time(() => { p.SToBytesBinary(); }, times, "BinarySerializer");
            //PerformanceHelper.Time(() => { p.SToBytesBson(); }, times, "BsonSerializer");
            PerformanceHelper.Time(() => { p.SToStringDataContract(); }, times, "DataContractSerializer");
            PerformanceHelper.Time(() => { p.SToStringJson(); }, times, "JsonSerializer");
            PerformanceHelper.Time(() => { p.SToBytesProtoBuf(); }, times, "ProtoBufSerializer");
            PerformanceHelper.Time(() => { p.SToStringXml(); }, times, "XmlSerializer");
            PerformanceHelper.Time(() => { p.SToStringJavaScript(); }, times, "JavaScriptSerializer");
            //PerformanceHelper.Time(() => { p.SToStringMongoJson(); }, times, "SToStringMongoJson");
            PerformanceHelper.Time(() => { p.SToBytesMongoBson(); }, times, "SToBytesMongoBson");
        }

        [TestMethod]
        public void TestIgnore()
        {
            Person p = new Person
            {
                A1 = "1233556778890",
                A2 = "1233556778890",
                A3 = "1233556778890",
                A4 = "1233556778890",
                A5 = "1233556778890",
                A6 = "1233556778890",
                A7 = "1233556778890",
                A8 = "1233556778890",
                B1 = 1233556778890,
                B2 = 1233556778890,
                B3 = 1233556778890,
                B4 = 1233556778890,
                B5 = 1233556778890,
                B6 = 1233556778890,
                B7 = 1233556778890,
                B8 = 1233556778890
            };
            string aaa = p.SToStringJson();
            aaa = p.SToStringXml();
            TestContext.WriteLine(aaa);
        }

        [TestMethod]
        public void Deserialize()
        {
            PerformanceHelper.OutputAction = (obj) => TestContext.WriteLine(obj.ToString());
            Person p = new Person
            {
                A1 = "1233556778890",
                A2 = "1233556778890",
                A3 = "1233556778890",
                A4 = "1233556778890",
                A5 = "1233556778890",
                A6 = "1233556778890",
                A7 = "1233556778890",
                A8 = "1233556778890",
                B1 = 1233556778890,
                B2 = 1233556778890,
                B3 = 1233556778890,
                B4 = 1233556778890,
                B5 = 1233556778890,
                B6 = 1233556778890,
                B7 = 1233556778890,
                B8 = 1233556778890
            };
            int times = 10000;
            var a1 = p.SToBytesBinary();
            PerformanceHelper.Time(() => { a1.SFromBytesBinary<Person>(); }, times, "BinarySerializer");
            //var a2 = p.SToBytesBson();
            //PerformanceHelper.Time(() => { a2.SFromBytesBson<Person>(); }, times, "BsonSerializer");
            var a3 = p.SToStringDataContract();
            PerformanceHelper.Time(() => { a3.SFromStringDataContract<Person>(); }, times, "DataContractSerializer");
            var a4 = p.SToStringJson();
            PerformanceHelper.Time(() => { a4.SFromStringJson<Person>(); }, times, "JsonSerializer");
            var a5 = p.SToBytesProtoBuf();
            PerformanceHelper.Time(() => { a5.SFromBytesProtoBuf<Person>(); }, times, "ProtoBufSerializer");
            var a6 = p.SToStringXml();
            PerformanceHelper.Time(() => { a6.SFromStringXml<Person>(); }, times, "XmlSerializer");
            var a7 = p.SToStringJavaScript();
            PerformanceHelper.Time(() => { a7.SFromStringJavaScript<Person>(); }, times, "JavaScriptSerializer");

            //var a8 = p.SToStringMongoJson();
            //PerformanceHelper.Time(() => { a8.SFromStringMongoJson<Person>(); }, times, "SToStringMongoJson");

            var a9 = p.SToBytesMongoBson();
            PerformanceHelper.Time(() => { a9.SFromBytesMongoBson<Person>(); }, times, "SToBytesMongoBson");

        }
    }

    [Serializable]
    [System.Runtime.Serialization.DataContract, ProtoBuf.ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
    public class Person
    {
        [DataMember]
        public string A1 { get; set; }
        [DataMember]
        public string A2 { get; set; }
        [DataMember]
        public string A3 { get; set; }
        [DataMember]
        public string A4 { get; set; }
        [DataMember]
        public string A5 { get; set; }
        [DataMember]
        public string A6 { get; set; }
        [DataMember]
        public string A7 { get; set; }
       
        [Framework.Common.Attributes.IgnoreDataMemberAttribute]
        public string A8 { get; set; }

        [DataMember]
        public long B1 { get; set; }
        [DataMember]
        public long B2 { get; set; }
        [DataMember]
        public long B3 { get; set; }
        [DataMember]
        public long B4 { get; set; }
        [DataMember]
        public long B5 { get; set; }
        [DataMember]
        public long B6 { get; set; }
        [DataMember]
        public long B7 { get; set; }
        [DataMember]
        public long B8 = 9;
    }
}
